import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.*;

public class test {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return dfs(inorder, postorder, map);
    }

    public TreeNode dfs(int[] inorder, int[] postorder, Map<Integer, Integer> map) {
        if(inorder.length == 0 || postorder.length == 0) return null;
        TreeNode node = new TreeNode(postorder[postorder.length - 1]);
        int idx = map.get(postorder[postorder.length - 1]);
        node.left = buildTree(Arrays.copyOfRange(inorder,0, idx), Arrays.copyOfRange(postorder, 0, idx));
        node.right = buildTree(Arrays.copyOfRange(inorder, idx + 1, inorder.length), Arrays.copyOfRange(postorder, idx, postorder.length - 1));
        return node;
    }
}